﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using KnowledgeCommunity.Web.Models;
using KnowledgeCommunity.Model;
using KnowledgeCommunity.Service;

namespace KnowledgeCommunity.Web.DbHelper
{
    public class ShopHelper
    {
        CKCDB mydb = new CKCDB();
        /// <summary>
        /// FindAll()
        /// </summary>
        /// <returns></returns>
        public IEnumerable<Shop> FindAll()
        {
            var apps = from n in mydb.Shops
                       orderby n.AddTime descending
                       select n;
            return apps;
        }
        /// <summary>
        /// FindById(int id)
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Shop FindById(int id)
        {
            var app = mydb.Shops.Find(id);
            return app;
        }

        /// <summary>
        /// FindByUserName(string user)
        /// </summary>
        /// <param name="user">用户名</param>
        /// <returns></returns>
        public IEnumerable<Shop> FindByUserName(string user)
        {
            var apps = from n in mydb.Shops
                       where n.UserName == user
                       orderby n.AddTime descending
                       select n;
            return apps;
        }

        public IEnumerable<Shop> FindShopByCommodityOwner(string user) 
        {
            var com1 = from n in mydb.Commodities
                       where n.UserName == user
                       select n;
            var apps=FindAll();
            List<Shop> myList = new List<Shop>();
            foreach (var item in com1) 
            {
                foreach (var i1 in apps) 
                {
                    if (i1.ShapeCode == item.ShapeCode) 
                    {
                        myList.Add(i1);
                    }
                }
            }
            return myList;
        }

        /// <summary>
        /// return 0-Fail; 1-Success
        /// </summary>
        /// <param name="model">Shop</param>
        /// <returns></returns>
        public bool Create(Shop model)
        {
            try
            {
                mydb.Shops.Add(model);
                mydb.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                string message = "Create in table<Shops>" + ex.ToString();
                WriteLog.WriteSysLog(message);
                return false;
            }
        }

        /// <summary>
        /// return 0-Fail; 1-Success
        /// </summary>
        /// <param name="model">Shop</param>
        /// <returns></returns>
        public bool Delete(int id)
        {
            var model = FindById(id);
            try
            {
                mydb.Shops.Remove(model);
                mydb.SaveChanges();
                return true;
            }
            catch (Exception ex)
            {
                string message = "Delete in table<Shops>" + ex.ToString();
                WriteLog.WriteSysLog(message);
                return false;
            }
        }
    }
}